home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / network / lattice / portlib.lzh / PORTLIB / PTY.C < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-16  |  4.3 KB  |  150 lines

  1. /*
  2.  * Mint version by Kay Roemer.
  3.  */
  4.  
  5. /*-
  6.  * Copyright (c) 1990 The Regents of the University of California.
  7.  * All rights reserved.
  8.  *
  9.  * Redistribution and use in source and binary forms, with or without
  10.  * modification, are permitted provided that the following conditions
  11.  * are met:
  12.  * 1. Redistributions of source code must retain the above copyright
  13.  *    notice, this list of conditions and the following disclaimer.
  14.  * 2. Redistributions in binary form must reproduce the above copyright
  15.  *    notice, this list of conditions and the following disclaimer in the
  16.  *    documentation and/or other materials provided with the distribution.
  17.  * 3. All advertising materials mentioning features or use of this software
  18.  *    must display the following acknowledgement:
  19.  *    This product includes software developed by the University of
  20.  *    California, Berkeley and its contributors.
  21.  * 4. Neither the name of the University nor the names of its contributors
  22.  *    may be used to endorse or promote products derived from this software
  23.  *    without specific prior written permission.
  24.  *
  25.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  26.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  29.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  30.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  31.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  32.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  33.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  34.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  35.  * SUCH DAMAGE.
  36.  */
  37.  
  38. #if defined(LIBC_SCCS) && !defined(lint)
  39. static char sccsid[] = "@(#)pty.c    5.6 (Berkeley) 5/10/91";
  40. #endif /* LIBC_SCCS and not lint */
  41.  
  42. #include <sys/types.h>
  43. #include <sys/stat.h>
  44. #include <sys/ioctl.h>
  45. #include <fcntl.h>
  46. #include <termios.h>
  47. #include <errno.h>
  48. #include <unistd.h>
  49. #include <stdio.h>
  50. #include <string.h>
  51. #include <grp.h>
  52. #include <osbind.h>
  53. #include <mintbind.h>
  54.  
  55. int
  56. openpty(amaster, aslave, name, termp, winp)
  57.     int *amaster, *aslave;
  58.     char *name;
  59.     struct termios *termp;
  60.     struct winsize *winp;
  61. {
  62.     static char line[] = "u:\\pipe\\ttyXX";
  63.     static char link[] = "u:\\dev\\ttyXX";
  64. #define PTYCHAR1    (sizeof (line)-3)
  65. #define PTYCHAR2    (sizeof (line)-2)
  66. #define LINKCHAR1    (sizeof (link)-3)
  67. #define LINKCHAR2    (sizeof (link)-2)
  68.     register const char *cp1, *cp2;
  69.     register int master, slave, ttygid;
  70.     struct group *gr;
  71.  
  72.     if ((gr = getgrnam("tty")) != NULL)
  73.         ttygid = gr->gr_gid;
  74.     else
  75.         ttygid = -1;
  76.  
  77.     for (cp1 = "pqrs"; *cp1; cp1++) {
  78.         line[PTYCHAR1] = *cp1;
  79.         for (cp2 = "0123456789abcdef"; *cp2; cp2++) {
  80.             struct stat st;
  81.  
  82.             line[PTYCHAR2] = *cp2;
  83.             if (stat (line, &st) == 0) continue;
  84.             if ((master = Fcreate (line,
  85.                 FA_SYSTEM|FA_HIDDEN)) < 0) {
  86.                 errno = ENOENT;
  87.                 return -1;
  88.             } else {
  89.                 (void) chown(line, getuid(), ttygid);
  90.                 (void) chmod(line, S_IRUSR|S_IWUSR|S_IWGRP);
  91.                 if ((slave = open(line, O_RDWR, 0)) != -1) {
  92.                     /* tesche: we're not interested in whether the link already existed or not */
  93.                     link[LINKCHAR1] = *cp1;
  94.                     link[LINKCHAR2] = *cp2;
  95.                     (void)Fsymlink(line, link);
  96.  
  97.                     *amaster = master;
  98.                     *aslave = slave;
  99.                     if (name) {
  100.                         extern int _dos2unx (char *,
  101.                             char *);
  102.                         _dos2unx (link, name);
  103.                     }
  104.                     if (termp)
  105.                         (void) tcsetattr(slave, 
  106.                             TCSAFLUSH, termp);
  107.                     if (winp)
  108.                         (void) ioctl(slave, TIOCSWINSZ, 
  109.                             (char *)winp);
  110.                     return (0);
  111.                 }
  112.                 (void) close(master);
  113.             }
  114.         }
  115.     }
  116.     errno = ENOENT;    /* out of ptys */
  117.     return (-1);
  118. }
  119.  
  120. int
  121. forkpty(amaster, name, termp, winp)
  122.     int *amaster;
  123.     char *name;
  124.     struct termios *termp;
  125.     struct winsize *winp;
  126. {
  127.     int master, slave, pid;
  128.     extern int login_tty (int);
  129.  
  130.     if (openpty(&master, &slave, name, termp, winp) == -1)
  131.         return (-1);
  132.     switch (pid = fork()) {
  133.     case -1:
  134.         return (-1);
  135.     case 0:
  136.         /* 
  137.          * child
  138.          */
  139.         (void) close(master);
  140.         login_tty(slave);
  141.         return (0);
  142.     }
  143.     /*
  144.      * parent
  145.      */
  146.     *amaster = master;
  147.     (void) close(slave);
  148.     return (pid);
  149. }
  150.